package com.rm.zeyou.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.CouponConfig;
import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.entity.UserCoupon;
import com.rm.zeyou.mapper.CouponConfigMapper;
import com.rm.zeyou.mapper.TechnicianMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.CouponConfigService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.distance.MathDistanceUtil;
import com.rm.zeyou.util.redis.RedisUtil;
import com.rm.zeyou.vo.request.SelectCouponListVo;
import com.rm.zeyou.vo.response.SelectCouponResponseBaseVo;
import com.rm.zeyou.vo.response.SelectCouponResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠卷配置表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:07
 */
@Slf4j
@Service
public class CouponConfigServiceImpl extends ServiceImpl<CouponConfigMapper, CouponConfig> implements CouponConfigService {

    @Resource
    private CouponConfigMapper couponMapper;

    @Resource
    private TechnicianMapper technicianMapper;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public ApiRestResponse<PageInfo<SelectCouponResponseVo>> selectCouponList(SelectCouponListVo selectVo) {
        PageHelper.startPage(selectVo.getPageNum(), selectVo.getPageSize());
        List<SelectCouponResponseVo> coupons = couponMapper.selectCouponList();
        if (CheckEmptyUtil.isEmpty(coupons)) {
            return ApiRestResponse.success(new PageInfo<SelectCouponResponseVo>());
        }
        PageInfo<SelectCouponResponseVo> source = new PageInfo<>(coupons);
        PageInfo<SelectCouponResponseVo> target = new PageInfo<SelectCouponResponseVo>();
        BeanUtils.copyProperties(source, target);
        List<SelectCouponResponseVo> voList = new ArrayList<>();
        List<SelectCouponResponseVo> list = source.getList();
        List<Long> tids = list.stream().map(SelectCouponResponseVo::getCouponScopeApplication).collect(Collectors.toList());

        list.forEach(item -> {
            SelectCouponResponseVo vo = new SelectCouponResponseVo();
            BeanUtils.copyProperties(item, vo);
            if (item.getCouponScopeApplication()==0) {
                //全场通用
                vo.setIsCanServer(NumberConstants.ONE_CONSTANTS);
                vo.setMark(new BigDecimal("5"));
                vo.setDistance("0");
                voList.add(vo);
            } else {
                Technician technician = technicianMapper.selectById(item.getCouponScopeApplication());
                String distance = MathDistanceUtil.getDistance(selectVo.getLongitude() + "," + selectVo.getLatitude(), technician.getLongitude() + "," + technician.getLatitude(), 2);
                BigDecimal distanceBig = new BigDecimal(distance).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP);

                //指定技师
                vo.setIsCanServer(NumberConstants.ONE_CONSTANTS);
                vo.setMark(technician.getMark());
                vo.setDistance(distanceBig.toPlainString());
                vo.setIsCanServer(technician.getIsOnline());
                vo.setTechnicianAvatar(technician.getTechnicianAvatar());
                vo.setMark(technician.getMark());
                vo.setDistance(String.valueOf(distanceBig));
                voList.add(vo);
            }

        });
        target.setList(voList);
        return ApiRestResponse.success(target);

    }


    @Override
    public ApiRestResponse<SelectCouponResponseBaseVo> selectNewCouponList(User user, SelectCouponListVo selectVo) {
        SelectCouponResponseBaseVo selectCouponResponseBaseVo = new SelectCouponResponseBaseVo();
        String redisKey = "user_coupon";
        String hget = redisUtil.hget(redisKey, user.getId().toString());
        if (CheckEmptyUtil.isEmpty(hget)) {
            selectCouponResponseBaseVo.setUserCouponCount(0);
            selectCouponResponseBaseVo.setCouponList(Collections.emptyList());
            return ApiRestResponse.success(selectCouponResponseBaseVo);
        }

        JSONObject jsonObject = JSONObject.parseObject(hget);
        selectCouponResponseBaseVo.setUserCouponCount(jsonObject.getInteger("newCouponCount"));
        List<SelectCouponResponseVo> couponList = new ArrayList<>();
        List<UserCoupon> newCouponList = jsonObject.getJSONArray("newCouponList").toJavaList(UserCoupon.class);
        if (!CheckEmptyUtil.isEmpty(newCouponList)) {
            for (UserCoupon userCoupon : newCouponList) {
                CouponConfig couponConfig = couponMapper.selectById(userCoupon.getCouponId());
                SelectCouponResponseVo vo = new SelectCouponResponseVo();
                vo.setCouponName(couponConfig.getCouponName());
                vo.setCouponFull(couponConfig.getCouponFull());
                vo.setCouponSubtract(couponConfig.getCouponSubtract());
                vo.setCloseDate(userCoupon.getCloseTime());
                couponList.add(vo);
            }
        }

        selectCouponResponseBaseVo.setCouponList(couponList);


        return ApiRestResponse.success(selectCouponResponseBaseVo);
    }


}
